import Module from "./str/Module";
import Unit from "./str/Unit";
import Domain from "./str/Domain";


const SysType={
    TYPE:0,

    SYS_CHAR:1, //符号
    SYS_LEX:2,  //词
    SYS_YACC:3, //句
    SYS_SWITCH:4, //转换
}

/**
 * 分词
 * 词法分析
 */
//  const LEX_TYPE={
//      TYPE:1,
//     LEX_WORD:1,  //词
//     LEX_NUMBER:2, //数
//     LEX_SYMBOL:3, //符号
//
//     LEX_REPEAT:4,  //重复词
//
//     LEX_OVER:5,
// }

export  const LEX_TYPE  = {
    LEX_OTHER:0,
    //   lex_REPEAT_NOUN: -22,  //重复名词
    //  LEX_OTHER_NUMBER1:-11,  //重复属于领域
    //  LEX_OTHER_NUMBER:-12,
    LEX_1LENG:1,
    LEX_1LENG_STRUC: 11, //结构
    LEX_2LENG:2,
    LEX_2LENG_NOUN:22,  //名词
    LEX_2LENG_VERB: 23, //动词
    LEX_3LENG:3,
    LEX_4LENG:4,
};

export   class LexNode{
    constructor( ){
        this.lexDomain=new Domain();
        this.lexDomain.addModule(new Module())
        this.lexDomain.addModule(new Module())
        this.lexDomain.addModule(new Module())
    }




    /**
     *分词
     */
    run(charModule){
        //字 数
        //是否是组合
        // 比如，结构，名词，动词
        let _this=this;
        let signs=[];
        let id=0;
        debugger
        for(let i=0;i<charModule.length;i++,id++){

            let wordUnitA=charModule[i];
            let wordUnitB=charModule[i+1];


            //两个字一样 重复字
            if( wordUnitA ==wordUnitB ){
                let wordIdz=wordUnitA;
                let list= new Module();
                while (wordUnitA==wordIdz) {
                    list.addUnit(new Unit('',charModule[i],YaccLengthType.YACC_REPEAT,(i),wordUnitA ))
                    i++;
                    wordUnitA=wordIds[i];
                }
                i--;
                signs.push(list)
                continue
            }


            let wordsId=_this.findWrodsId(wordUnitA,wordUnitB)
            if(wordsId.find(1)!=""){
                if(_this.isSignOne(wordsId, LEX_TYPE.LEX_2LENG_NOUN)){

                    let wordIdC=wordIds[i+2];
                    let isWords3=_this.findWrodsId(wordUnitB,wordIdC)
                    let isWords3B=false;
                    if (isWords3.find(1)!=""){
                        if(_this.isSignOne(wordIdC,LEX_TYPE.LEX_1LENG_STRUC)||_this.isSignOne(wordIdC,LEX_TYPE.LEX_2LENG_NOUN))   isWords3B=true
                    }

                    if(isWords3B||_this.isSignOne(wordIdC,LEX_TYPE.LEX_2LENG_NOUN)){
                        let list=new Module();
                        list.addUnit(new Unit('',charModule[i],LEX_TYPE.LEX_4LENG,i));
                        list.addUnit(new Unit('',charModule[i+1],LEX_TYPE.LEX_4LENG,(i+1)));
                        list.addUnit(new Unit('',charModule[i+2],LEX_TYPE.LEX_4LENG,(i+2)));
                        list.addUnit(new Unit('',charModule[i+3],LEX_TYPE.LEX_4LENG,(i+3)));
                        i=i+2;
                        signs.push(list)
                        continue
                    }
                    let list=new Module();
                    list.addUnit(new Unit('',charModule[i],LEX_TYPE.LEX_2LENG,(i)));
                    list.addUnit(new Unit('',charModule[i+1],LEX_TYPE.LEX_2LENG,(i+1)));
                    signs.push(list)
                    i++;
                    continue
                }

            }



            /**
             * 如果字符有特殊标记
             */

            if(_this.isSignOne(wordUnitA,LEX_TYPE.LEX_SYMBOL)){
                //遇到特殊标记开始检查栈数据是否完整。
                //检查清空栈

                switch(wordSign) {
                    //数字
                    case '1':

                        while (wordSign!=数字) {
                            id.push(wordId)
                            i++;
                           let wordId=wordIds[i];
                          //  wordSign= _this.findSign(wordId)
                        }
                        signs.push({id:id,remarks:数字})
                        i--;
                        break;
                    case '1': //标点符号

                        while (wordSign!=数字) {
                            id.push(wordId)
                            i++;
                            let  wordId=wordIds[i];
                          //  wordSign= _this.findSign(wordId)
                        }
                        signs.push({id:id,remarks:数字})
                        i--;
                        break;
                    case '1'://特殊字符

                        while (wordSign!=数字) {
                            id.push(wordId)
                            i++;
                            let wordId=wordIds[i];
                         //   wordSign= _this.findSign(wordId)
                        }
                        signs.push({id:id,remarks:数字})
                        i--;
                        break
                    default:
                        let list=new Module();
                        list.addUnit(new Unit('',charModule[i],LEX_TYPE.LEX_SYMBOL,(i)));
                        signs.push(list)
                        break
                }
                continue
            }
            /**
             * 检查 wordUnitB 是不是有没有特殊标记B
             */
            if(_this.isSignOne(wordUnitB,LEX_TYPE.LEX_1LENG_STRUC)){
                let list=new Module();
                list.addUnit(new Unit('',charModule[i],LEX_TYPE.LEX_1LENG,(i)));
                signs.push(list)

                let listb=new Module();
                listb.addUnit(new Unit('',charModule[i+1],LEX_TYPE.LEX_SYMBOL,(i+1)));
                signs.push(listb)

                i++;
                continue
            }


            let wordIdC=charModule[i+2];

            if(_this.isSignOne(wordIdC,LEX_TYPE.LEX_2LENG_NOUN)){
                let list=new Module();
                list.addUnit(new Unit('',charModule[i],LEX_TYPE.LEX_3LENG,(i)));
                list.addUnit(new Unit('',charModule[i+1],LEX_TYPE.LEX_3LENG,(i+1)));

                list.addUnit(new Unit('',charModule[i+2],LEX_TYPE.LEX_3LENG,(i+2)));
                signs.push(list)
                i=i+2;
                continue
            }

            let list=new Module();
            list.addUnit(new Unit('',charModule[i],LEX_TYPE.LEX_2LENG,(i)));
            list.addUnit(new Unit('',charModule[i+1],LEX_TYPE.LEX_2LENG,(i+1)));
            signs.push(list)
            i++;
            continue
        }
        return signs


    }
    /**
     * 检查是否 分词标记
     * @param wordSign
     */
    isSignOne(wordSign,type ){
        /**
         *  数字  digit
         *  符号 other
         *  单位 unit
         *
         * @type {Array}
         */
       let  units=   this.lexDomain.findUnit(SysType.SYS_LEX,wordSign,"",1)
        if(units.length<1){
            return false
        }
        let sunits=   this.lexDomain.findUnit(units.find(1),wordSign,"",type)

        if(sunits.length>+1){
            return  true;
        }
        return false;
    }

    /**
     * 检查是否 分词标记
     * @param wordSign
     */
    isSign(wordSign,type ){
        /**
         *  数字  digit
         *  符号 other
         *  单位 unit
         *
         * @type {Array}
         */
        let units=  wordSign.isUnis("",type,"",LEX_TYPE.TYPE)
        if(units.length>1){
            return true
        }


        return false;
    }

    /**
     * 查找字符 的标记
     * @param wordId
     * @returns {null}
     */
    findSign(wordId){
        debugger
        return  this.lexDomain.find(wordId.find(1));
    }


    //查询是不是的词
    findWrodsId(wordUnitA,wordUnitB){
        let wrodsIds=this.lexDomain.findUnit(SysType.SYS_LEX,"",wordUnitA ,wordUnitB ,2);
        if(wrodsIds.length>0){
            return  wrodsIds[0];
        }else {
            return new Unit('', wordUnitA,wordUnitB,2) ;
        }
    }




//添加词
   addIder( charModule ){
       let wordModule=this.lexDomain.find(SysType.SYS_LEX);
       let unitA=charModule.find(0);
       let unitB=charModule.find(0);

       let wrodsIds=  wordModule.isUnits('', unitA.find(1),unitB.find(1),SysType.SYS_LEX)
        let unitZ= wrodsIds[0];
       let id="";
       if(wrodsIds.length>0){
           id=unitZ.find(1);
       }else {
           let unit=  new Unit("",unitA.find(1),unitB.find(1),type)
           id=  this.lexDomain.moduleAddUnit(SysType.SYS_LEX, unit)
       }
       return id;
   }


    //添加语法类型
    /**
     *
     * @param id  意 的id
     * @param domainNum  领域编码
     * @param resNum  返回编码
     * @param methodtype 方法类型
     * @returns {*}
     */
     iderAddType(ids,domainId,methodNum, methodType){
         let wordModule=this.lexDomain.find(SysType.SYS_LEX);
        let id=0;
        let units;
        switch (ids.length){
            case 1:
                units=   this.lexDomain.findUnit(SysType.SYS_LEX,ids[0],"",1)
                if(units.length>1){
                    let unit1 = units[0];
                    id = unit1.find(1);
                }else {
                    let unit2 = new Unit("",ids[0],"",1);
                   id= this.lexDomain.addModuleRow(SysType.SYS_LEX,unit2)
                }
                break;
            case 2:
                units=   this.lexDomain.findUnit(SysType.SYS_LEX,ids[0],ids[1],2)
                if(units.length>1){
                    let unit1 = units[0];
                    id = unit1.find(1);
                }else {

                    let unit2 = new Unit("",ids[0],ids[1],2);
                    id= this.lexDomain.addModuleRow(SysType.SYS_LEX,unit2)
                }
                break;
            default:
                return "";
        }




         let wrodsIds=  wordModule.isUnits(id, domainId,methodNum,methodType)

         if(wrodsIds.length>0){
             let unitZ= wrodsIds[0];
             id=unitZ.find(1);
         }else {
             let unit=  new Unit("",domainId,methodNum,methodType)
             id=  this.lexDomain.moduleAddUnit(id, unit)
         }
         return id;
     }

}





